package com.hz.friends.location;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Timer;

import java.util.TimerTask;

import com.hz.friends.global.Global;
import com.hz.friends.utils.MLog;

 

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class LocationService extends Service {
	private LocationManager lm;

	private MyLocationlistener netLocationListener;
	private MyLocationlistener gpsLocationListener;
	private String TAG = this.getClass().getSimpleName();

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initLocation(this);
	}
 

	@Override
	public void onDestroy() {
		if (lm != null && netLocationListener != null) {
			lm.removeUpdates(netLocationListener);
		}
		if (lm != null && gpsLocationListener != null) {
			lm.removeUpdates(gpsLocationListener);
		}
		super.onDestroy();
	}

 
	private void initLocation(Context mContext) {
		lm = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
				&& !lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			startBroadCast(0,0,"not_open_set");
		}
		if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			netLocationListener = new MyLocationlistener();
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0,
					netLocationListener);

		}
		if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			gpsLocationListener = new MyLocationlistener();
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0,
					gpsLocationListener);
		}
	}
	   /** 
     * 通過經緯度獲取地址 
     *  
     * @param point 
     * @return 
     */  
    private String getLocationAddress(double lat,double lon) {  
        String add = "";  
        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());  
        try {  
            List<Address> addresses = geoCoder.getFromLocation(lat, lon,  1);  
            Address address = addresses.get(0);  
            int maxLine = address.getMaxAddressLineIndex(); 
            if (maxLine >= 2) {  
                add = address.getAddressLine(0) + address.getAddressLine(1);  
                add = address.getAddressLine(0) +address.getAdminArea();

            } else {  
                add = address.getAddressLine(1);  
            }  
        } catch (IOException e) {  
            add = "";  
            e.printStackTrace();  
        }  
       MLog.i(TAG,"====location address:"+add);
        return add;  
    }  
    private  Handler mhandler = new Handler();
    
	private class MyLocationlistener implements LocationListener {

		@Override
		public void onLocationChanged(final Location location) {
			MLog.i(TAG, "[onLocationChanged]  lat:"+location.getLatitude() +" lon"+location.getLongitude()
					+" "+location.getProvider());
			
			mhandler.post(new Runnable() {
				
				@Override
				public void run() {
				   String address = getLocationAddress(location.getLatitude(),location.getLongitude());
				   startBroadCast(location.getLatitude(),location.getLongitude(),address);
				}
			});
			   startBroadCast(location.getLatitude(),location.getLongitude(),null);
		}

		@Override
		public void onProviderDisabled(String provider) {

		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}
	
	private void startBroadCast(double lat,double lon,String address){
		Intent intent = new Intent(Global.LOCAT_ACTION);
		intent.putExtra("address", address);
		intent.putExtra("lat", lat);
		intent.putExtra("lon", lon);
		sendBroadcast(intent);
		
	}
}